#include <QPushButton>
#include <QLabel>
#include <QDebug>

#include "cvtabwidget.h"
#include "contentview.h"
#include "bizlogic/commentmanager.h"

CVTabWidget::CVTabWidget(QWidget *parent) :
    QTabWidget(parent)
{
    //setTabsClosable(true);
}

void CVTabWidget::addBlogTab(Blog *blog, const QString &msg)
{
    ContentView *view = new ContentView();
    view->setUserIcon(blog->getOwnerIcon());
    view->setTitle(blog->getTitle());
    view->setDescription(blog->getDescription());
    view->setOwner(blog->getOwner());
    view->setCommentCount(blog->getCommentCount());

    addTab(view, blog->getTitle());
    addTabCloseButton();

    CommentManager *commentMgr = new CommentManager();
    commentMgr->getBlogComments(blog->getId());
    connect(commentMgr, SIGNAL(handleComments(QList<Comment*>*)), view, SLOT(setComments(QList<Comment*>*)));
}

void CVTabWidget::addTopicTab(Topic *topic, const QString &msg)
{
    ContentView *view = new ContentView();
    view->setUserIcon(topic->getOwnerIcon());
    view->setTitle(topic->getTitle());
    view->setDescription(topic->getDescription());
    view->setOwner(topic->getOwner());
    qDebug() << topic->getDescription();
    addTab(view, topic->getTitle());
    addTabCloseButton();

    CommentManager *commentMgr = new CommentManager();
    commentMgr->getTopicComments(topic->getId());
    connect(commentMgr, SIGNAL(handleComments(QList<Comment*>*)), view, SLOT(setComments(QList<Comment*>*)));
}

void CVTabWidget::closeTab(ClickableLabel *label)
{
    qDebug() << "[CVTabWidget::closeTab] Tab Close label clicked :" << this->tabBar()->currentIndex();
    removeTab(this->tabBar()->currentIndex());
}

void CVTabWidget::addTabCloseButton()
{
    ClickableLabel *closeLb = new ClickableLabel();
    QImage image(":/images/closetab");
    closeLb->setPixmap(QPixmap::fromImage(image));
    int curIndex = this->tabBar()->count() - 1;
    //closeLb->addData("index", QString::number(curIndex));
    this->tabBar()->setTabButton(curIndex, QTabBar::RightSide, closeLb);
    connect(closeLb, SIGNAL(clicked(ClickableLabel*)), this, SLOT(closeTab(ClickableLabel*)));
}
